/*
 * Copyright (c) 2013, Klaus Hauschild
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided
 * that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice, this list of conditions and
 *     the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
 *     and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package de.hauschild.utilsn.core;

import playn.core.CanvasImage;
import playn.core.Font;
import playn.core.Font.Style;
import playn.core.Layer;
import playn.core.PlayN;
import playn.core.TextFormat;
import playn.core.TextLayout;

import com.google.common.base.Preconditions;

/**
 * @since 0.1.0
 * 
 * @author Klaus Hauschild
 */
public class TextBuilder {

  public static TextBuilder newBuilder() {
    return new TextBuilder();
  }

  private Font font;
  private Integer fontColor;
  private String fontName;
  private int fontSize;
  private Style fontStyle;
  private TextFormat textFormat;

  private TextBuilder() {
  }

  public Layer create(final String text) {
    if (font == null) {
      Preconditions.checkNotNull(fontName);
      Preconditions.checkNotNull(fontSize);
      if (fontStyle == null) {
        fontStyle = Style.PLAIN;
      }
      font = PlayN.graphics().createFont(fontName, fontStyle, fontSize);
    }
    if (textFormat == null) {
      Preconditions.checkNotNull(font);
      textFormat = new TextFormat().withFont(font);
    }
    final TextLayout layout = PlayN.graphics().layoutText(text, textFormat);
    final CanvasImage image = PlayN.graphics().createImage((int) Math.ceil(layout.width()),
        (int) Math.ceil(layout.height()));
    if (fontColor != null) {
      image.canvas().setFillColor(fontColor);
    }
    image.canvas().fillText(layout, 0, 0);
    return PlayN.graphics().createImageLayer(image);
  }

  public TextBuilder withFont(final Font font) {
    this.font = font;
    return this;
  }

  public TextBuilder withFontColor(final Integer fontColor) {
    this.fontColor = fontColor;
    return this;
  }

  public TextBuilder withFontName(final String fontName) {
    this.fontName = fontName;
    return this;
  }

  public TextBuilder withFontSize(final int fontSize) {
    this.fontSize = fontSize;
    return this;
  }

  public TextBuilder withStyle(final Style fontStyle) {
    this.fontStyle = fontStyle;
    return this;
  }

  public TextBuilder withTextFormat(final TextFormat textFormat) {
    this.textFormat = textFormat;
    return this;
  }

}
